package com.html.utils;

import com.alibaba.fastjson.JSONObject;
import com.html.dto.ElementDTO;
import com.html.dto.URLElementMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.protocol.HTTP;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: HTMLUtil
 * @Author: Jack.Zhang
 * @Description:
 * @Date: 2021-09-27
 */
public class HTMLUtil {
    private HTMLUtil(){}


    /***
      * <p>
      *    获取某个URL页面所有html节点信息
      * <p/>
      *
	  * @param requestUrl  请求URL
      * @return com.html.dto.URLElementMap
      * @author Jack.Zhang 2021/9/27 10:18
      * @since 1.0.0
      * @see
      */
    public static URLElementMap getURLElementMap(String requestUrl) {
        InputStream inputStream = null;
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection connection =
                    (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");
            inputStream =
                    connection.getInputStream();
            byte[] bytes = new byte[8 * 1024];
            StringBuilder stringBuilder = new StringBuilder();
            while (-1 != inputStream.read(bytes)) {
                stringBuilder.append(new String(bytes, "UTF-8"));
                bytes = new byte[8 * 1024];
            }
            List<ElementDTO> elementList = getElementList(stringBuilder.toString());
            return new URLElementMap(requestUrl, elementList);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("请求出现异常："+requestUrl);
        }finally {
            if (Objects.nonNull(inputStream)){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static List<ElementDTO> getElementList(String html) {
        final Document document = Jsoup.parse(html);
        final List<Node> nodeList = document.childNodes();
        return getElement(nodeList);
    }


    private static List<ElementDTO> getElement(List<Node> nodeList) {
        if (CollectionUtils.isEmpty(nodeList)) {
            return new ArrayList<>();
        }
        List<ElementDTO> resultList = new ArrayList<>();
        nodeList.forEach(node->{
            ElementDTO elementDTO = new ElementDTO();
            final String tagName = node.nodeName();
            elementDTO.setTagName(tagName);
            final Attributes attributes = node.attributes();
            if (Objects.nonNull(attributes)) {
                final Map<String, String> attributeMap = attributes.asList().stream()
                        .collect(Collectors.toMap(Attribute::getKey, Attribute::getValue));
                elementDTO.setParamsMap(attributeMap);
            }
            elementDTO.setContent(node.toString());
            resultList.add(elementDTO);
            if (!CollectionUtils.isEmpty(node.childNodes())) {
                // 不是最后一级标签
                resultList.addAll( getElement(node.childNodes()) );
            }
        });
        return resultList;
    }
}
